package com.renting.modules.renting.rentSys.service;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ImageUtil;
import cn.hutool.core.util.RandomUtil;
import com.renting.modules.renting.rentSys.dao.HouseMapper;
import com.renting.modules.renting.rentSys.entity.HouseImage;
import com.renting.modules.upyun.dao.ImageDao;
import main.java.com.UpYun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.renting.common.entity.LocationEntity;
import com.renting.modules.renting.rentSys.dao.HouseDao;
import com.renting.modules.renting.rentSys.entity.House;
import sun.misc.BASE64Decoder;

/**
 * @author administrator
 * @version 1.0 @remark：
 * @create date： 2018年3月10日
 */
@Service
public class HouseService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    UpYun upYun;

    @Autowired
    ImageDao imageDao;

    @Value("${up_bucket_url}")
    private String UP_BUCKET_URL;//服务器前缀


    /**
     * houses
     *
     * @param house
     * @return
     * @description:通过id查询单个房源信息
     * @exception:查询不到房源信息 返回null
     * @author administrator
     * @create date： 2018年3月11日 @remark：
     */
    public House houses(House house) {
        Optional<House> optHouse = houseDao.findById(house.getId());// 查询出房源信息
        if (!optHouse.isPresent()) {// 如果查询不到返回null
            return null;
        }
        house = optHouse.get();
        return house;
    }

    /**
     * getNearbyHouses
     *
     * @param location
     * @return
     * @description:获取附近房源
     * @exception:
     * @author administrator
     * @create date： 2018年3月16日 @remark：
     */
    public List<House> getNearbyHouses(LocationEntity location) {
        List<House> houses = houseDao.getNearbyHouses(location.getLgt(), location.getLat());//得到放进房源
        return houses;
    }


    /**
     * 包含分页 条件查询 排序
     * mybatis
     *
     * @param house
     * @return
     */
    public List<House> getHouses(House house) {
        return houseMapper.findHouses(house);
    }

    /**
     * 保存house 包括保
     *
     * @param house
     */
    public void save(House house) {
        house.getImages().forEach(img -> {
            String imgId = RandomUtil.simpleUUID();
            String mouthDir = DateUtil.format(new Date(), "yyyy-MM");
            String serverUrl = "/" + mouthDir + "/" + imgId + ".jpg";
            boolean isUpload = upYun.writeFile(serverUrl, Base64.decode(img.getVisitUrl().substring(23)));
            if(isUpload){//图片上传成功
                HouseImage houseImage = new HouseImage();
                houseImage.setId(imgId);
                houseImage.setHouseId(house.getId());
                houseImage.setServerUrl(serverUrl);
                houseImage.setVisitUrl(UP_BUCKET_URL+"/"+serverUrl);//网络路径
                imageDao.save(houseImage);
            }
        });
        houseDao.save(house);
    }
}
